//
//  YFHomeViewController.swift
//  YFSwiftWeibo
//
//  Created by tsaievan on 2017/8/24.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit

fileprivate let originalCellId = "originalCellId"
fileprivate let retweetedCellId = "retweetedCellId"

class YFHomeViewController: YFBaseViewController {
    
    fileprivate lazy var statusListViewModel = YFWeiboStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    // MARK: -navigationBar左边的按钮点击事件
    @objc fileprivate func showFriend() {
        let vc = YFDemoViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
}

extension YFHomeViewController {
    override func setupTableView () {
        super.setupTableView()
        _navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriend))
        tableView?.register(UINib(nibName: "YFStatusNormalCell", bundle: nil), forCellReuseIdentifier: originalCellId)
        tableView?.register(UINib(nibName: "YFStatusRetweetedCell", bundle: nil), forCellReuseIdentifier: retweetedCellId)
        
        ///< 去掉tableViewCell的分隔线
        tableView?.separatorStyle = .none
        setupNavTitle()
    }
    
    override func loadData() {
        
        statusListViewModel.loadStatus(isPullUp: isPullUp) { (isSuccess, shouldRefresh) in
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5) {
                /// 有两点需要注意:
                /// 1. 刷新控件结束刷新
                self.refreshControl?.endRefreshing()
                
                /// 2. 恢复上拉刷新控件
                // 这里必须要保证, 下拉刷新一定能取到数据, 不然改成false之后, 会一直不停地刷新
                self.isPullUp = false
                
                /// 3. tableView刷新数据, 如果shouldRefresh为true,就刷新, 否则就什么都不做
                if shouldRefresh {
                    self.tableView?.reloadData()
                }
            }
        }
    }
}

extension YFHomeViewController {
    func setupNavTitle() {
        let title = YFNetworkManager.shared.tokenModel.screen_name
        let button = YFTitleButton(title: title)
        button.addTarget(self, action: #selector(navTitleClick), for: .touchUpInside)
        _navItem.titleView = button
    }
}


// MARK: - 事件处理
extension YFHomeViewController {
    ///< 导航栏标题点击
    @objc fileprivate func navTitleClick(sender: UIButton) {
        sender.isSelected = !sender.isSelected
    }
}


// MARK: -实现tableView的代理方法和数据源方法
extension YFHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let viewModel = statusListViewModel.statusList[indexPath.row]
        ///< 根据不同的微博(原创/转发)来采用不同的cellId
        let cellId = (viewModel.status.retweeted_status != nil) ? retweetedCellId : originalCellId
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! YFStatusCell
        cell.viewModel = viewModel
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let vm = statusListViewModel.statusList[indexPath.row]
        return vm.rowHeight
        
    }
}
